package Filesharing;

import ibatis.IbatisHelper;

import java.util.*;
import java.sql.*;

import Eclass.Batch.BatchModel;

import com.ibatis.sqlmap.client.SqlMapClient;

public class FileManager {
	private SqlMapClient ibatis;
	
	public FileManager() {
		this.ibatis = IbatisHelper.getSqlMapInstance();
	}
	
	public List selectAllFile(FileForm fileShareForm) {
		List result = null;
		
		try {
			Map map = new HashMap();
			map.put("param_fileName", fileShareForm.getParam_fileName());
			map.put("param_desc", fileShareForm.getParam_description());
			map.put("param_uploaderId", fileShareForm.getParam_uploaderId());
			map.put("param_upload", fileShareForm.getParam_dtmUpload());
			map.put("param_upd", fileShareForm.getParam_dtmupd());
			result = ibatis.queryForList("filesharing.getFile", map);
			
			/*for(int i=0;i<result.size();i++){
				FileModel fm = (FileModel) result.get(0);
				System.out.println(fm);
			}*/
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return result;
	}
	
	public boolean addFile(FileModel bean) {
		try {
			ibatis.startTransaction();
			//System.out.println("Add File");
			//bean.toString();
			this.ibatis.insert("filesharing.insert", bean);
			ibatis.commitTransaction();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				ibatis.endTransaction();
			} catch (Exception e2) {
				// TODO: handle exception
				e2.printStackTrace();
			}
		}
		
		return true;
	}
	
	public boolean addFileSch(FileModel bean) {
		try {
			ibatis.startTransaction();
			//System.out.println("Add File");
			//bean.toString();
			this.ibatis.insert("filesharing.insertFileSch", bean);
			ibatis.commitTransaction();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				ibatis.endTransaction();
			} catch (Exception e2) {
				// TODO: handle exception
				e2.printStackTrace();
			}
		}
		
		return true;
	}
	
	public FileModel selectFile(String idFile) {
		FileModel result = new FileModel();
		try {
			result = (FileModel) ibatis.queryForObject("filesharing.getSelectedFile", idFile);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return result;
	}
	
	//utk sharing materi (schedule)
	public List selectFileSch(Integer batchClassId) {
		List result = null;
		try {
			result = ibatis.queryForList("filesharing.getSelectedFileByBatchClsId", batchClassId);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		return result;
	}
	
	public boolean updateFile(FileModel bean) {
		try {
			ibatis.startTransaction();
			this.ibatis.update("filesharing.update", bean);
			ibatis.commitTransaction();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				ibatis.endTransaction();
			} catch (Exception e2) {
				// TODO: handle exception
				e2.printStackTrace();
			}
		}
				
		return true;
	}
	
	public boolean updateFileSch(FileModel bean) {
		try {
			ibatis.startTransaction();
			this.ibatis.update("filesharing.updateFileSch", bean);
			ibatis.commitTransaction();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				ibatis.endTransaction();
			} catch (Exception e2) {
				// TODO: handle exception
				e2.printStackTrace();
			}
		}
				
		return true;
	}
	
	public boolean deleteFile(String idFile) {
		try {
			ibatis.startTransaction();
			this.ibatis.delete("filesharing.delete", idFile);
			ibatis.commitTransaction();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				ibatis.endTransaction();
			} catch (Exception e2) {
				// TODO: handle exception
				e2.printStackTrace();
			}
		}
				
		return true;
	}
	
	//utk renaming file dgn nama yg sama
	public Integer getCurFileId() {
		Integer result = null;
		
		try {
			result = (Integer) ibatis.queryForObject("filesharing.getCurFileId", "");
		} catch (SQLException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		return result;
	}
	
	
}
